package com.shy.db.controller;

import com.shy.api.model.dto.KqNoticeResultDto;
import com.shy.api.model.dto.NoticeResultDto;
import com.shy.api.model.query.KqNoticeQuery;
import com.shy.db.service.ThirdPartApiService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class NotifyController {

    @Resource
    private Map<String, ThirdPartApiService> serviceMap;

    /**
     * 快钱异步通知
     *
     * @return
     */
    @GetMapping("/notice/kq")
    public String notifyKq(KqNoticeQuery kqNoticeQuery) {
        String pageUrl = "http://localhost:8206/gateway/showResult.html";
        try {
            //验证签名
            boolean checkSign = kqNoticeQuery.checkSign();
            System.out.println("验证快钱签名"+checkSign);

            checkSign = true;
            if( !checkSign){
                throw new RuntimeException("验证签名失败");
            }

            //调用业务方法，处理异步通知
            ThirdPartApiService apiService = serviceMap.get("KUAIQIAN");
            if( apiService == null){
                throw new RuntimeException("服务无效");
            }
            KqNoticeResultDto resultDto = (KqNoticeResultDto) apiService.handleNotice(kqNoticeQuery);
            pageUrl = resultDto.getPageUrl();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "<result>1</result><redirecturl>" +  pageUrl +  "</redirecturl>";
    }
}
